/*
 * Copyright (C) 2009 M. Hautle.
 * 
 * This file is part of TrackedBean.
 * 
 * TrackedBean is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * TrackedBean is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with TrackedBean. If not, see <http://www.gnu.org/licenses/>.
 */
package ch.trackedbean.binding.guicomponents;

import java.awt.*;
import javax.swing.*;
import ch.trackedbean.binding.beanSpec.*;
import ch.trackedbean.binding.mapping.*;

/**
 * Label which changes it's color according to a linked bean property status
 * 
 * @author M. Hautle
 */
public class StatusLabel extends JLabel implements IStatusDependent {
	/** Color indicating an technical error. */
	public static final Color TECHNICAL_ERROR = Color.RED.darker();

	/** Color indicating an error. */
	public static final Color ERROR = Color.RED;

	/** Color indicating an changed value. */
	public static final Color CHANGED = Color.BLUE.darker();

	/**
	 * Creates a <code>StatusLabel</code> instance with the specified text, image, and horizontal alignment. The label is centered vertically in its display
	 * area. The text is on the trailing edge of the image.
	 * 
	 * @param text The text to be displayed by the label.
	 * @param icon The image to be displayed by the label.
	 * @param horizontalAlignment One of the following constants defined in <code>SwingConstants</code>: <code>LEFT</code>, <code>CENTER</code>,
	 *            <code>RIGHT</code>, <code>LEADING</code> or <code>TRAILING</code>.
	 */
	public StatusLabel(String text, Icon icon, int horizontalAlignment) {
		super(text, icon, horizontalAlignment);
	}

	/**
	 * Creates a <code>StatusLabel</code> instance with the specified text and horizontal alignment. The label is centered vertically in its display area.
	 * 
	 * @param text The text to be displayed by the label.
	 * @param horizontalAlignment One of the following constants defined in <code>SwingConstants</code>: <code>LEFT</code>, <code>CENTER</code>,
	 *            <code>RIGHT</code>, <code>LEADING</code> or <code>TRAILING</code>.
	 */
	public StatusLabel(String text, int horizontalAlignment) {
		super(text, horizontalAlignment);
	}

	/**
	 * Creates a <code>StatusLabel</code> instance with the specified text. The label is aligned against the leading edge of its display area, and centered
	 * vertically.
	 * 
	 * @param text The text to be displayed by the label.
	 */
	public StatusLabel(String text) {
		super(text);
	}

	/**
	 * Creates a <code>StatusLabel</code> instance with the specified image and horizontal alignment. The label is centered vertically in its display area.
	 * 
	 * @param image The image to be displayed by the label.
	 * @param horizontalAlignment One of the following constants defined in <code>SwingConstants</code>: <code>LEFT</code>, <code>CENTER</code>,
	 *            <code>RIGHT</code>, <code>LEADING</code> or <code>TRAILING</code>.
	 */
	public StatusLabel(Icon image, int horizontalAlignment) {
		super(image, horizontalAlignment);
	}

	/**
	 * Creates a <code>StatusLabel</code> instance with the specified image. The label is centered vertically and horizontally in its display area.
	 * 
	 * @param image The image to be displayed by the label.
	 */
	public StatusLabel(Icon image) {
		super(image);
	}

	/**
	 * Creates a <code>StatusLabel</code> instance with no image and with an empty string for the title. The label is centered vertically in its display area.
	 * The label's contents, once set, will be displayed on the leading edge of the label's display area.
	 */
	public StatusLabel() {
		super();
	}

	/**
	 * Creates a {@link StatusLabel} and attaches it to the given {@link MappingGroup}.
	 * 
	 * @param text The label text
	 * @param property The property for which this label is used
	 * @param grp The mapping group where to attach this label
	 * @return The labal
	 */
	public static StatusLabel create(final String text, final String property, final MappingGroup grp) {
		final StatusLabel b = new StatusLabel(text);
		grp.createBinding(property, b);
		return b;
	}

	/**
	 * Sets the label color according to a bean property status
	 * 
	 * @param status The status of a bean property
	 */
	@Override
	public void setStatus(final Status status) {
		Color c;
		switch (status.getFlag()) {
			case CHANGED:
				c = CHANGED;
				break;
			case ERROR:
				c = ERROR;
				break;
			case TECHNICAL_ERROR:
				c = TECHNICAL_ERROR;
				break;
			default:
				c = Color.BLACK;
		}
		setToolTipText(status.getMessage());
		setForeground(c);
	}
}
